<template>
  <view :style="colorStyle">
    <view
      class="infobox1 flex aitems"
      :style="{ height: `${menuInfo.top + menuInfo.height}px` }"
    >
      <view class="block2 flex aitems" @click="back">
        <image src="@/static/img14.png" class="back" mode=""></image>
      </view>
      <view
        class="block1 flex aitems"
        @click="gourl(`/pages/seller/search?id=${user.id}`)"
      >
        <view class="icon">
          <u-icon name="search" size="24" color="#fff"></u-icon>
        </view>
        <view class="input cfff f28"> 搜索 </view>
      </view>
    </view>
    <view class="bg">
      <image :src="user.backImg" mode="aspectFill"></image>
    </view>

    <view class="infobox2 flex aitems" :style="navTitleModuleStyle2">
      <image :src="user.logo" mode="aspectFill"></image>
      <view class="block1">
        <view class="info1 cfff f36 fontWeight">
          {{ user.name }}
        </view>
        <view class="info2 flex aitems">
          <image
            src="@/static/img7.png"
            mode=""
            v-for="(item, index, key) in user.score"
            :key="'score' + index"
          ></image>
          <image
            src="@/static/img47.png"
            mode=""
            v-for="(item, index, key) in 5 - user.score"
            :key="'score' + index"
          ></image>
        </view>
      </view>
    </view>

    <view class="" style="height: 436rpx"> </view>
    <block v-if="!loadingPage">
      <x-skeleton
        type="list"
        :loading="true"
        v-for="(item, indexs, key) in 10"
        :key="key"
      >
        <view>列表</view>
      </x-skeleton>
    </block>
    <view class="couponModule" v-if="loadingPage">
      <view class="info1">
        <view
          v-for="(item, index, key) in list"
          :key="key"
          class="list flex aitems"
        >
          <view class="left">
            <view class="price">
              <span class="unit">￥</span>
              <span class="num">{{ item.money }}</span>
            </view>
            <view class="price1" v-if="item.minPrice > 0">
              满{{ item.minPrice }}元使用
            </view>
            <view class="price1" v-if="item.minPrice <= 0"> 无门槛 </view>
            <view class="label"> {{ couponsUseType[item.useType] }} </view>
          </view>
          <view class="border"></view>
          <view class="right flex aitems between">
            <view class="couponinfo">
              <view class="name fontWeight"> {{ item.name }} </view>
              <view class="time" v-if="item.isFixedTime == 1">
                有效期至 {{ item.useEndTime }}
              </view>
              <view class="time" v-if="item.isFixedTime == 0">
                有效期至 {{ item.day }}
              </view>
              <view
                class="log flex aitems"
                @click="couponDescriptionChange(item)"
              >
                <u-icon name="search" size="22" color="#525252"></u-icon>
                <span>查看使用说明</span>
              </view>
            </view>
            <view
              class="couponbtn"
              @click="save(item)"
              :style="{ background: `${item.isGet == 1 ? '#ccc' : ''}` }"
            >
              {{ couponIsGet[item.isGet] }}
            </view>
          </view>
        </view>
      </view>
    </view>

    <nodata v-if="list.length == 0" />
    <view class="" style="height: 100rpx"> </view>

    <custom-tab-bar
      :activeIndex="2"
      :sellerId="user.id"
      :user="user"
      :list="[]"
    ></custom-tab-bar>

    <u-popup
      mode="bottom"
      :safeAreaInsetBottom="false"
      :show="show"
      :overlayStyle="{ 'touch-action': 'none' }"
      bgColor="transparent"
      @close="show = false"
    >
      <coupon-description :value="info.brief" @closeChange="show = false" />
    </u-popup>
  </view>
</template>

<script>
import customTabBar from "./tabbar.vue";
import { getToken, setStorage, getStorage } from "@/utils/storage.js";
import { sellerInfo, couponPage, couponReceive } from "@/servers/servers.js";
import { HTTP_STATUS } from "@/servers/config.js";
import colors from "@/mixins/color";
import { loading, go, back, msg } from "@/utils/wxAPI";
import { formatDate } from "@/utils/util";
import { couponsUseType, couponIsGet } from "@/keys/keys";
export default {
  mixins: [colors],
  components: {
    customTabBar,
  },
  data() {
    return {
      show: false,
      couponsUseType,
      couponIsGet,
      menuInfo: {},
      navTitleModuleStyle: "",
      navTitleModuleStyle2: "",
      user: {
        score: 0,
      },
      options: {},

      screenType: "",
      priceType: "",
      saleType: "",
      params: {},
      loadingPage: false,
      total: 0,
      list: [],
      current: 1,
      viewType: 1, //1列表 2左右
      info: {},
    };
  },
  async onLoad(options) {
    this.options = options;
    // #ifdef MP-WEIXIN
    this.menuInfo = getApp().globalData.menuInfo;
    this.navTitleModuleStyle = `top:${
      this.menuInfo.height + this.menuInfo.top + 10
    }px`;
    this.navTitleModuleStyle2 = `top:calc(${
      this.menuInfo.height + this.menuInfo.top
    }px)`;
    // #endif
    // #ifdef H5
    this.navTitleModuleStyle2 = `top:calc(100rpx)`;
    // #endif

    if (getToken()) {
      this.sellerInfo();
      this.couponPage();
    }
  },
  methods: {
    couponDescriptionChange(info) {
      this.info = info;
      this.show = true;
    },
    async sellerInfo() {
      loading();
      let { data } = await sellerInfo({
        id: this.options.id,
      });
      if (data.code == HTTP_STATUS.RUSELT_SUCCESS) {
        let info = data.data;
        this.user = info;
      }
      uni.hideLoading();
    },
    async couponPage() {
      loading();
      let { data } = await couponPage({
        current: this.current,
        size: 10,
        status: 1,
        sellerId: this.options.id,
      });
      if (data.code == HTTP_STATUS.RUSELT_SUCCESS) {
        let list = data.data.records;
        this.total = data.data.total;
        list.map((item) => {
          if (item.isFixedTime == 0) {
            let time1 =
              new Date(item.createTime.replace(/-/g, "/")).getTime() +
              1000 * 60 * 60 * 24 * item.day;
            item.day = formatDate(time1).dateValue;
          }
        });
        if (this.current == 1) {
          this.list = list;
        } else {
          this.list.push(...list);
        }
        setTimeout(() => {
          this.loadingPage = true;
        }, 500);
      }
      uni.stopPullDownRefresh();
      uni.hideLoading();
    },
    async save(item) {
      loading();
      let { data } = await couponReceive({
        id: item.id,
      });
      if (data.code == HTTP_STATUS.RUSELT_SUCCESS) {
        msg(data.msg);
        this.current = 1;
        this.couponPage();
      }
    },
    back() {
      back();
    },
    gourl(url) {
      go(url);
    },
  },
  onReachBottom() {
    if (this.list.length == this.total) return;
    this.current += 1;
    this.couponPage();
  },
  onPullDownRefresh() {
    this.current = 1;
    this.couponPage();
  },
};
</script>
<style>
/* #ifdef MP-WEIXIN */
page {
  background: #f5f5f5;
}

/* #endif */
</style>
<style lang="scss" scoped>
page {
  background: #f5f5f5;
}

.bg {
  position: fixed;
  top: 0;
  width: 100%;
  height: 436rpx;
  z-index: 999;

  image {
    width: 100%;
    height: 100%;
  }
}

.infobox1 {
  // background: #fff;
  padding: 0 0 22rpx 0;
  position: fixed;
  width: 100%;
  z-index: 99999;
  /* #ifdef H5 */
  height: 90rpx;
  /* #endif */

  .block2 {
    position: absolute;
    bottom: 26rpx;
    left: 30rpx;
    z-index: 999;

    .back {
      width: 48rpx;
      height: 48rpx;
    }

    .headimg {
      width: 64rpx;
      height: 64rpx;
      border-radius: 57rpx;
      margin-left: 10rpx;
    }
  }

  .block1 {
    border-radius: 44rpx;
    background: rgba(255, 255, 255, 0.4);
    width: 430rpx;
    /* #ifdef H5 */
    width: calc(100% - 30rpx - 30rpx - 48rpx - 40rpx);
    /* #endif */
    position: absolute;
    bottom: 18rpx;
    padding: 0 4rpx;
    left: calc(20rpx + 48rpx + 40rpx);
    height: 64rpx;

    .icon {
      margin-left: 28rpx;
    }

    .input {
      margin-left: 16rpx;
    }

    .btn {
      position: absolute;
      right: 0;
      border-radius: 44rpx;
      background: var(--view-theme);
      padding: 15rpx 47rpx;
    }
  }
}

.infobox2 {
  position: fixed;
  margin: 0 30rpx 0;
  z-index: 999;
  padding: 54rpx 0 0;
  width: calc(100% - 30rpx - 54rpx);

  image {
    width: 88rpx;
    height: 88rpx;
    border-radius: 16rpx;
  }

  .block1 {
    margin-left: 16rpx;
    width: calc(100% - 16rpx - 88rpx);

    .info1 {
    }

    .info2 {
      margin: 8rpx 0 0;

      image {
        width: 30rpx;
        height: 30rpx;
        margin: 0 12rpx 0 0;
      }
    }
  }
}

.infobox3 {
  position: fixed;
  top: 0;
  width: 100%;
  height: 436rpx;
  z-index: 999;

  .block2 {
    // margin: 40rpx 0 0;
    position: absolute;
    bottom: 24rpx;
    width: 100%;

    .info1 {
      .icon {
        width: 30rpx;
        height: 30rpx;
      }

      .tip1 {
      }

      .arrow-down-fill {
        margin-left: 12rpx;
      }
    }
  }
}

.goodsModule {
  margin: 30rpx 30rpx 0;

  .goodslist {
    background: #fff;
    border-radius: 24rpx;
    padding: 16rpx;
    margin: 0 0 24rpx 0;

    .goodsimg {
      width: 230rpx;
      height: 230rpx;
      border-radius: 24rpx;
    }

    .goodsinfo {
      width: calc(100% - 230rpx - 16rpx);
      margin-left: 16rpx;
      height: 260rpx;

      .block1 {
        width: 100%;

        .goodsname {
        }

        .goodslabel {
          margin: 16rpx 0;

          .label {
            color: var(--view-theme);
            padding: 4rpx 18rpx;
            border: 1px solid var(--view-theme);
            border-radius: 8rpx;
            margin: 0 20rpx 0 0;
          }
        }
      }

      .block2 {
        color: var(--view-theme);

        .unit {
          position: relative;
          top: 16rpx;
        }

        .price {
        }
      }
    }
  }

  .goodslist2 {
    background: #fff;
    border-radius: 24rpx;
    padding: 16rpx 0;
    width: 335rpx;
    margin: 0 0 24rpx 0;

    .goodsimg {
      width: 303rpx;
      height: 303rpx;
      border-radius: 24rpx;
      margin: auto;
    }

    .goodsinfo {
      width: 303rpx;
      margin: auto;
      padding: 16rpx 0 0;
      // width: calc(100% - 230rpx - 16rpx);
      // margin-left: 16rpx;
      // height: 260rpx;

      .block1 {
        width: 100%;

        .goodsname {
        }

        .goodslabel {
          margin: 16rpx 0;

          .label {
            color: var(--view-theme);
            padding: 4rpx 18rpx;
            border: 1px solid var(--view-theme);
            border-radius: 8rpx;
            margin: 0 20rpx 0 0;
          }
        }
      }

      .block2 {
        color: var(--view-theme);

        .unit {
          position: relative;
          top: 16rpx;
        }

        .price {
        }
      }
    }
  }
}

.couponModule {
  position: relative;
  overflow: hidden;
  margin: 30rpx 30rpx 0;

  .info1 {
    position: relative;
    z-index: 1;

    .list {
      padding: 20rpx 32rpx;
      background: #fff;
      border-radius: 28rpx;
      margin: 0 0 24rpx 0;

      .left {
        width: 160rpx;
        text-align: center;

        .price {
          color: #1f1f1f;
          font-weight: bold;

          .unit {
            font-size: 40rpx;
          }

          .num {
            font-size: 64rpx;
          }
        }

        .price1 {
          font-size: 24rpx;
          color: #1f1f1f;
        }

        .label {
          margin: 16rpx auto 0;
          color: var(--view-theme);
          font-size: 24rpx;
          border-radius: 10rpx;
          border: 2rpx solid var(--view-theme);
          background: var(--view-minorColorT);
          width: 140rpx;
          height: 42rpx;
          line-height: 42rpx;
          text-align: center;
        }
      }

      .border {
        border-left: 2rpx dashed #858585;
        width: 2rpx;
        height: 160rpx;
        margin: 0 20rpx;
      }

      .right {
        width: calc(100% - 160rpx - 44rpx);

        .couponinfo {
          width: 250rpx;

          .name {
            color: #1f1f1f;
            font-size: 32rpx;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
          }

          .time {
            color: #525252;
            font-size: 24rpx;
            margin: 24rpx 0 0;
          }

          .log {
            color: #525252;
            font-size: 24rpx;
            margin-top: 8rpx;

            span {
              margin-left: 4rpx;
            }
          }
        }

        .couponbtn {
          border-radius: 40rpx;
          background: var(--view-theme);
          display: flex;
          padding: 12rpx 30rpx;
          justify-content: center;
          align-items: center;
          gap: 10rpx;
          color: #fff;
          font-size: 24rpx;
          //   margin-left: 0rpx;
        }
      }
    }
  }
}
</style>
